package com.vancouver.knows.ui.fragment.citynews;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import butterknife.Bind;
import butterknife.OnClick;

import com.astuetz.PagerSlidingTabStrip;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.ui.activity.citynews.AddPostActivity;
import com.vancouver.knows.ui.activity.search.NewSearchActivity;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;

/**
 * 城事
 * 
 * @author chenxunlin01
 * 
 */
public class CityNewsFragment extends BaseFragment {  // implements  LoadableList.LoadableListScrollListener

//	@Bind(R.id.toolbar)
//	Toolbar mToolbar;

//	@Bind(R.id.citynews)
//	CityNewsPullControllerView cityNewsPullControllerView;

//	RadioGroup mTabGroup;
//	@Bind(R.id.pager)
//	ViewPager viewPager;
//	SparseArray<Fragment> fragmentMap = new SparseArray<Fragment>();
//	private int pageNum;
//	private CityNewsPagerAdapter cityNewsPagerAdapter;
//	private int toIndex = -1;


	@Bind(R.id.tabStrip)
	PagerSlidingTabStrip indicator;
	@Bind(R.id.pager)
	ViewPager viewPager;
	@Bind(R.id.scroll_shadow_left)
	ImageView scrollShadowLeft;
	@Bind(R.id.scroll_shadow_right)
	ImageView scrollShadowRight;

	SparseArray<Fragment> fragmentMap = new SparseArray<Fragment>();
	private String[] titles;
	private int pageNum;
	private CityNewsPagerAdapter cityNewsPagerAdapter;
	private int toIndex = -1;

	public static CityNewsFragment newInstance() {
		return new CityNewsFragment();
	}

	@Override
	protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_citynews, container, false);
	}

	@Override
	public void onResume() {
		super.onResume();

		Tracker t = ((MainApplication) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_CITYNEWS_FRAGMENT);
		t.send(new HitBuilders.ScreenViewBuilder().build());
	}

	public void onAddPostClick() {
		Tracker t = ((MainApplication) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_CITYNEWS);
		t.send(new HitBuilders.EventBuilder().setCategory(AnalyticsConstants.CATEGORY_BTN)
				.setAction(AnalyticsConstants.ACTION_ONCLICK).setLabel(AnalyticsConstants.LABEL_CITYNEWS_ADD_POST)
				.build());

		if (AppUtils.getLoginStatus(getActivity())) {
			startActivity(new Intent(getActivity(), AddPostActivity.class));
		}
	}

//	@OnClick(R.id.search_icon)
//	public void onSearchIconClick() {
//		Intent searchIntent = new Intent(getActivity(), NewSearchActivity.class);
//		startActivity(searchIntent);
//	}

//	@Override
//	protected void bindView(View rootView) {
//		MainApplication imContext = ((MainApplication) getActivity().getApplication());
//		ATaskMark aTaskMark = imContext.getBusinessModule().getTaskMarkPool().getFetchCityNewsTaskMarkList("", "");
//		cityNewsPullControllerView.loadData(aTaskMark);
//	}
//
//	public void scrollUp() {
//		cityNewsPullControllerView.scrollUp();
//	}
//
//	@Override
//	public void subHandleMessage(Message msg) {
//		switch (msg.what) {
//			case Constants.BUSINESS_STATUS_USER_UPVOTE:
//			case Constants.BUSINESS_STATUS_USER_DELETE_UPVOTE:
//			case Constants.BUSINESS_STATUS_USER_DEL_ARTICLE:
//			case Constants.BUSINESS_STATUS_CITYNEWS_COMMENT_COUNT_CHANGE:
//				cityNewsPullControllerView.flush();
//				break;
//		}
//	}

//	@Override
//	public void onScrollUp() {
//		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
//		viewPropertyAnimator.setListener(new AnimatorListener() {
//			@Override
//			public void onAnimationStart(Animator animation) {
//				mToolbar.setVisibility(View.VISIBLE);
//			}
//		});
//		viewPropertyAnimator.translationY(0).setInterpolator(new AccelerateInterpolator()).start();
//	}
//
//	@Override
//	public void onScrollDown() {
//		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
//		viewPropertyAnimator.setListener(new AnimatorListener() {
//			@Override
//			public void onAnimationStart(Animator animation) {
//				mToolbar.setVisibility(View.GONE);
//			}
//		});
//		viewPropertyAnimator.translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
//	}

	public void setToIndex(int toIndex) {
		this.toIndex = toIndex;
	}
//
//	@Override
//	public void onCheckedChanged(RadioGroup group, int checkedId) {
//		if (checkedId == R.id.top_tab) {
//			viewPager.setCurrentItem(0);
//		} else if (checkedId == R.id.latest_tab) {
//			viewPager.setCurrentItem(1);
//		}
//	}

	@Override
	protected void bindView(View rootView) {

		titles = getResources().getStringArray(R.array.citynews_tab_titles);
		pageNum = titles.length;
		cityNewsPagerAdapter = new CityNewsPagerAdapter(getChildFragmentManager());
		viewPager.setAdapter(cityNewsPagerAdapter);
		indicator.setViewPager(viewPager);

		if (toIndex != -1) {
			viewPager.setCurrentItem(toIndex);
			toIndex = -1;
		}

		// setup scroll shadows for the pager title strip
		scrollShadowLeft.setVisibility(View.GONE);
		scrollShadowRight.setVisibility(View.VISIBLE);
		indicator.setPagerScrollListener(new PagerSlidingTabStrip.PagerScrollListener() {
			@Override
			public void onScrollChanged(PagerSlidingTabStrip view, int x, int y, int oldx, int oldy) {

				View lastView = view.getChildAt(view.getChildCount() - 1);
				int diff = lastView.getRight() - (view.getWidth() + view.getScrollX() + lastView.getLeft());
				if (diff <= 0) {
					// reached end of pager title strip
					scrollShadowLeft.setVisibility(View.VISIBLE);
					scrollShadowRight.setVisibility(View.GONE);
				} else if (view.getScrollX() <= 0) {
					// reached beginning of pager title strip
					scrollShadowLeft.setVisibility(View.GONE);
					scrollShadowRight.setVisibility(View.VISIBLE);
				} else {
					scrollShadowLeft.setVisibility(View.VISIBLE);
					scrollShadowRight.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	public void scrollUp() {
		int currentIndex = viewPager.getCurrentItem();
		CityNewsChildrenFragment cityNewsChildrenFragment = (CityNewsChildrenFragment) cityNewsPagerAdapter.getItem(currentIndex);
		cityNewsChildrenFragment.scrollUp();
	}


	public class CityNewsPagerAdapter extends FragmentStatePagerAdapter {

		public CityNewsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Fragment fragment = (Fragment) super.instantiateItem(container, position);
			Fragment mFragment = fragmentMap.get(position);
			if (mFragment == null) {
				fragmentMap.put(position, fragment);
			}
			return fragment;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = fragmentMap.get(position);
			if (fragment == null) {
				fragment = createFragment(position);
				fragmentMap.put(position, fragment);
			}
			return fragment;
		}

		@Override
		public int getCount() {
			return pageNum;
		}

		private Fragment createFragment(int position) {
			int type = CityNewsChildrenFragment.CITYNEWS_TYPE_HOT;

			switch (position) {
			case 0:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_HOT;
				break;
            case 1:
                type = CityNewsChildrenFragment.CITYNEWS_TYPE_MUSIC_MATCH;
                break;
			case 2:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_NEWEST;
				break;
			case 3:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_INFORMATION;
				break;
			case 4:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_IRRIGATION;
				break;
			case 5:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_SECONDHAND;
				break;
			case 6:
				type = CityNewsChildrenFragment.CITYNEWS_TYPE_SHIPING;
				break;
			}

			return CityNewsChildrenFragment.newInstance(type);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

	}

}
